package com.dlm.config;

import java.util.Properties;

import com.dlm.controller.*;
import com.dlm.dao.*;
import com.dlm.sdk.api.ApiConfigKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;

public class WeixinConfig extends JFinalConfig {

    public Properties loadProp(String pro, String dev) {
        try {
            return loadPropertyFile(pro);
        } catch (Exception e) {
            return loadPropertyFile(dev);
        }
    }

    public void configConstant(Constants me) {
        // 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
        loadProp("config.properties", "config.properties");
        me.setDevMode(getPropertyToBoolean("devMode", false));

        // ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
        ApiConfigKit.setDevMode(me.getDevMode());
    }

    public void configRoute(Routes me) {
        me.add("/", ItemController.class);
        me.add("/msg", WeixinMsgController.class);
        me.add("/api", WeixinApiController.class, "/api");
        me.add("/item", ItemController.class);
        me.add("/tech", TechnicianController.class);
        me.add("/order", OrderController.class);
        me.add("/evaluate", EvaluateController.class);
        me.add("/ucoupon", UserCouponController.class);
        me.add("/sms", SMSController.class);
        me.add("/addr", HisAddressController.class);
    }

    public void configPlugin(Plugins me) {
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
        me.add(c3p0Plugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);

        arp.addMapping("users", Users.class);
        arp.addMapping("coupon", Coupon.class);
        arp.addMapping("evaluate", Evaluate.class);
        arp.addMapping("favorit", Favorit.class);
        arp.addMapping("his_address", HisAddress.class);
        arp.addMapping("item", Item.class);
        arp.addMapping("order", Order.class);
        arp.addMapping("order_item", OrderItem.class);
        arp.addMapping("work_time", WorkTime.class);
        arp.addMapping("technician", Technician.class);
        arp.addMapping("user_coupon", UserCoupon.class);
        arp.addMapping("t_item", TItem.class);

        EhCachePlugin ecp = new EhCachePlugin();
        me.add(ecp);
    }

    public void configInterceptor(Interceptors me) {

    }

    public void configHandler(Handlers me) {

    }

    public static void main(String[] args) {
        JFinal.start("webapp", 80, "/", 5);
    }
}
